/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: xsocket.h
 *
 * Purpose: wrapper socket operation API
 *
 * Developer:
 *   wen.gu , 2023-07-10
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __XBUS_CORE_XSOCKET_H__
#define __XBUS_CORE_XSOCKET_H__

#include <cstdint>
#include <string>
#include <functional>
#include <map>

#include "xbus/core/xtype.h"
#include "xbus/core/xerror.h"
#include "xbus/core/xresult.h"


#define INVALID_XSOCKET_ID (-1)

namespace xbus::core {

enum class XSocketType: uint8_t {
    None = 0,
    UDS,
    TCP,
    UDP,
    Max,
};

using XSocketId = int64_t;

enum class XSocketCmd: uint32_t {
    None = 0,
};

struct XSocketCmdParam {
    XSocketCmd cmd_;
    uint32_t size_;
    XSocketCmdParam(XSocketCmd cmd, uint32_t size):cmd_(cmd), size_(size) { }
};

template<typename SubType, XSocketCmd cmd>
struct XSocketCmdParamImpl: public XSocketCmdParam {
    XSocketCmdParamImpl(): XSocketCmdParam(cmd, sizeof(SubType)) { }
};


struct XSocketRecvResult {
    uint32_t recv_size;
    XSocketId sock_id;
};


#if 0
XSocketId XSocketOpen(XSocketType type);
core::XErrC XSocketClose(XSocketId sock_id);

/** for client side */
core::XErrC XSocketConnect(XSocketId sock_id, const std::string& url, int32_t port = 0);

/** for server side */
core::XErrC XSocketBind(XSocketId sock_id, const std::string& url, int32_t port = 0);
XSocketId XSocketAccept(XSocketId sock_id, int32_t timeout_ms = core::WaitType::WaitForever); /** for orainted-connection protocol(e.g. tcp, uds) */


int32_t XSocketRecv(XSocketId sock_id, uint8_t* buf, uint32_t size, int32_t timeout_ms = core::WaitType::WaitForever);
int32_t XSocketSend(XSocketId sock_id, const uint8_t* buf, uint32_t size);

int32_t XSocketRecvFrom(XSocketId sock_id, XSocketId & src_fd, uint8_t* buf, uint32_t size, int32_t timeout_ms = core::WaitType::WaitForever);
int32_t XSocketSendTo(XSocketId sock_id, XSocketId dst_fd, const uint8_t* buf, uint32_t size);  

core::XResultCode XSocketSetOpt(XSocketId sock_id, const XSocketCmdParam& param);
core::XResultCode XSocketGetOpt(XSocketId sock_id, XSocketCmdParam& param);

/** for udp only */
XSocketId XSocketMakeDstSocketId(const std::string& url, int32_t port);



#else
/////////////////////////////////////////////////////////////////////////////////////////

core::XResult<XSocketId> XSocketOpen(XSocketType type, bool as_server, const std::string& url, int32_t port = 0);
core::XResultCode XSocketClose(XSocketId sock_id);

/** for server side, only for orainted-connection protocol(e.g. tcp, uds) */
core::XResult<XSocketId> XSocketAccept(XSocketId sock_id, int32_t timeout_ms = core::WaitType::WaitForever);

core::XResult<XSocketRecvResult> XSocketRecv(XSocketId sock_id, uint8_t* buf, uint32_t size, int32_t timeout_ms = core::WaitType::WaitForever);

/** dst_fd: is valid only for udp protocol */
core::XResultCode XSocketSend(XSocketId sock_id, const uint8_t* buf, uint32_t size, XSocketId dst_fd = INVALID_XSOCKET_ID);

core::XResultCode XSocketSetOpt(XSocketId sock_id, const XSocketCmdParam& param);
core::XResultCode XSocketGetOpt(XSocketId sock_id, XSocketCmdParam& param);

/** for udp */
XSocketId XSocketMakeDstSocketId(const std::string& url, int32_t port);

#endif

} //namespace xbus::core


#endif /** !__XBUS_CORE_XSOCKET_H__ */